package org.geektimes.projects.user.web.controller;

import org.geektimes.context.ComponentContext;
import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.DatabaseUserRepository;
import org.geektimes.projects.user.repository.UserRepository;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.sql.DBConnectionManager;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

/**
 * @author: fengabner
 * @date: 2021/3/2 22:24
 * @version: 1.0
 * @description:
 */
@Path("/user")
public class UserController implements PageController {

    @POST
    @Path("/register")
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        User user = new User();
        user.setEmail(request.getParameter("email"));
        user.setPassword(request.getParameter("password"));
        UserService userService = ComponentContext.getInstance().getComponent("bean/UserService");
        boolean result = false;
        String view = "error.jsp";
        String info;
        try {
            result = userService.register(user);
        } catch (Exception e) {
        }
        if (result) {
            view = "success.jsp";
            info = user.getEmail();
        } else {
            info = "该用户已存在，请重新注册";
        }
        request.setAttribute("info", info);

        return view;
    }
}
